package feng.demo.service;

import feng.demo.dao.CommodityDao;
import feng.demo.dao.RemarkDao;
import feng.demo.dao.TypeDao;
import feng.demo.dao.UserDao;
import feng.demo.entity.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ScanOptions;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

@Service
@CacheConfig(cacheNames = {"commodity"})
public class CommodityServiceImpl implements CommodityService{
    @Autowired
    CommodityDao commodityDao;
    @Autowired
    TypeDao typeDao;
    @Autowired
    RemarkDao remarkDao;
    @Autowired
    UserDao userDao;
    @Autowired
    RedisTemplate redisTemplate;
    @Override
    @Cacheable
    public List<Commodity> findAll() {
        List<Commodity> list = commodityDao.findAll();
        return doadds(list);
    }

    @Override
    @Cacheable
    public Commodity findById(Integer comid) {
        Commodity commodity = commodityDao.findById(comid);

        return doadd(commodity);
    }

    @Override
    @Cacheable
    public List<Commodity> findByUid(Integer uid) {
        List<Commodity> list = commodityDao.findByUid(uid);
        return doadds(list);
    }

    @Override
    @Cacheable
    public List<Commodity> findByTypeid(Integer typeid) {
        List<Commodity> list = commodityDao.findByTypeid(typeid);
        return doadds(list);
    }

    @Override
    @Cacheable
    public List<Commodity> findAllLimit(Integer pagenum, Integer lines) {
        List<Commodity> list = commodityDao.findAllLimit(pagenum, lines);
        return doadds(list);
    }

    @Override
    @Cacheable
    public List<Commodity> findByIdLimit(Integer comid, Integer pagenum, Integer lines) {
        List<Commodity> list = commodityDao.findByIdLimit(comid, pagenum, lines);
        return doadds(list);
    }

    @Override
    @Cacheable
    public List<Commodity> findByUidLimit(Integer uid, Integer pagenum, Integer lines) {
        List<Commodity> list = commodityDao.findByUidLimit(uid, pagenum, lines);
        return doadds(list);
    }

    @Override
    @Cacheable
    public List<Commodity> findByTypeidLimit(Integer typeid, Integer pagenum, Integer lines) {
        List<Commodity> list = commodityDao.findByTypeidLimit(typeid, pagenum, lines);
        return doadds(list);
    }

    @Override
    public List<Commodity> findByCommodity(Map<String, Object> map) {
        List<Commodity> list = commodityDao.findByCommodity(map);
        return doadds(list);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(value = "commodity", allEntries = true)
    public int add(Commodity commodity) {
        int add = commodityDao.add(commodity);
        Type type = commodity.getType();
        type.setTnum(1);
        typeDao.update(type);
        return add;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(value = "commodity", allEntries = true)
    public int del(Integer comid) {
        Commodity commodity = commodityDao.findById(comid);
        Type type = commodity.getType();
        type.setTnum(-1);
        typeDao.update(type);
        int del = commodityDao.del(comid);
        return del;
    }

    @Override
    @CacheEvict(value = "commodity", allEntries = true)
    public int update(Commodity commodity) {
        int update = commodityDao.update(commodity);
        return update;
    }

    @Override
    @Cacheable
    public List<Commodity> findBySort(Integer pagenum, Integer lines) {
        List<Commodity> list = commodityDao.findBySort(pagenum, lines);
        return doadds(list);
    }

    @Override
    public int CountAll() {
        return commodityDao.CountAll();
    }

    @Override
    public int CountByTypeid(Integer typeid) {
        return commodityDao.CountByTypeid(typeid);
    }

    @Override
    public int CountByUid(Integer uid) {
        return commodityDao.CountByUid(uid);
    }

    @Override
    @Cacheable
    public List<Commodity> findRemark(Integer uid,Integer pagenum,Integer lines) {
        List<Commodity> list = commodityDao.findByUidLimit(uid,pagenum,lines);
       return doadds(list);
    }

    @Override
    @Cacheable
    public List<Commodity> findByque(String s) {
        List<Commodity> list = commodityDao.findByque(s);
        return doadds(list);
    }

    @Override
    @Cacheable
    public List<Commodity> findByCusUid(Integer uid, Integer pagenum, Integer lines) {
        List<Commodity> list = commodityDao.findByCusUid(uid, pagenum, lines);
        return doadds(list);
    }

    @Override
    public int CountByCusUid(Integer uid) {
        return commodityDao.CountByCusUid(uid);
    }

    private List<Commodity> doadds(List<Commodity> list){
        for (Commodity c:list) {
            User u = userDao.findByUid(c.getUser().getUid());
            List<Remark> remarks = remarkDao.findByComid(c.getComid());
            for (Remark r:remarks) {
                User user = userDao.findByUid(r.getUser().getUid());
                User us = userDao.findByUid(r.getCus().getUid());
                r.setUser(user);
                r.setCus(us);
            }
            c.setRemarks(remarks);
            c.setUser(u);
        }
        return list;

    }
    private Commodity doadd(Commodity c){
        User u = userDao.findByUid(c.getUser().getUid());
        List<Remark> remarks = remarkDao.findByComid(c.getComid());

        for (Remark r:remarks) {
            User user = userDao.findByUid(r.getUser().getUid());
            r.setUser(user);
        }
        c.setRemarks(remarks);
        c.setUser(u);
        return c;
    }
}
